package com.i2works.smartluolong.manage.tags;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.i2works.smartluolong.manage.entity.admin.AdminMenuDO;
import com.i2works.smartluolong.manage.entity.admin.AdminMenuQuery;
import com.i2works.smartluolong.manage.service.admin.IAdminMenuService;
import com.i2works.smartluolong.utils.entity.Tree;
import com.i2works.smartluolong.utils.shiro.spring.SpringMVCUtils;
import com.i2works.smartluolong.web.entity.common.TreeUtil;
import org.beetl.core.GeneralVarTagBinding;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Scope;

import java.util.List;
import java.util.Map;

/**
 * 左侧菜单标签库
 *
 * @author koal <koal@vip.qq.com>
 * @date 2017/4/1 9:12
 */

@Scope("prototype")
public class MenuTags extends GeneralVarTagBinding {
    private final Logger logger = LoggerFactory.getLogger(getClass());

    /**
     * 传递status参数
     * 1：显示已启用的
     * 0：显示已禁用的
     * 不写status参数，显示全部
     */
    @Override
    public void render() {
        try {
            JSONObject json = new JSONObject();
            //获取标签参数
            Map attrs = (Map) args[1];
            if (null != attrs && attrs.size() > 0) {
                for (Object key : attrs.keySet()) {
                    json.put(key.toString(), attrs.get(key));
                }
            }

            IAdminMenuService adminMenuService = SpringMVCUtils.getBean("adminMenuService");
            AdminMenuQuery query = JSONObject.parseObject(String.valueOf(json), AdminMenuQuery.class);
            List<AdminMenuDO> menuList = adminMenuService.queryAllList(query);
            List<Tree> treeDataList = Lists.newArrayList();
            for (AdminMenuDO menuDO : menuList) {
                Tree treeData = new Tree();
                treeData.setId(menuDO.getId());
                treeData.setPid(menuDO.getPid());
                treeData.setIconCls(menuDO.getMenuIcon());
                treeData.setText(menuDO.getMenuName());
                treeData.setUrl(menuDO.getMenuUrl());
                treeDataList.add(treeData);
            }
            List<Tree> nodeTree = TreeUtil.getNodeTree(0, treeDataList);
            //添加到前台
            this.binds(nodeTree);
            this.doBodyRender();
        } catch (Exception e) {
            logger.error("tags.MenuTags异常：", e);
            //e.printStackTrace();
        }
    }
 
}
